/*
 * Created on Nov 13, 2004 by Ming Chow
 *
 * Receives signal when touch sensor is pressed
 */

import java.awt.*;
import javax.swing.*;
import java.io.*;

import josx.rcxcomm.RCXBean;

public class SensorControl1Client extends JFrame
{
	public static void main(String[] args)
	{
		new SensorControl1Client();
	}
	
	public SensorControl1Client()
	{
		setSize(300, 75);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    Container content = getContentPane();
	    content.setBackground(Color.white);
		DisplayMessagePanel msgPanel=new DisplayMessagePanel();
	    content.add(msgPanel);
	    setVisible(true);
	    try
	    {
	    	RCXBean rcxb=new RCXBean();
	      	rcxb.setComPort("COM1");
			while (true)
			{
		      	int signal=rcxb.receiveInt();
				if (signal==1)
				{
					msgPanel.incrementNumPressed();
					msgPanel.repaint();
				}
			}
		}
	    catch (IOException ioe) {};	  
	}
}

class DisplayMessagePanel extends JPanel
{
	private int numPressed;
	
	public DisplayMessagePanel()
	{
		numPressed=0;
	}
	
	public void incrementNumPressed()
	{
		numPressed++;
	}
	
	public void paint(Graphics g)
	{
		g.clearRect(0, 0, 300, 75);
		String message="Touch sensor was pressed "+numPressed+" times.";
		g.drawString(message, 20, 20);
	}
}
